/*
 * The MIT License 
 *
 * Copyright (c) 2010 pf-miles
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package me.wenyue.jclassview.util;

import java.lang.reflect.Array;

/**
 * some common array related operations
 * 
 * @author pf-miles 2010-11-17 下午05:38:25
 * 
 */
public class ArrayUtil {
    /**
     * 
     * re-arranges the passed-in array to a table with 16 columns
     * 
     * @param <T>
     *            data type inside the array and table
     * @param array
     *            array to be converted
     * @return a table with 16 columns
     */
    @SuppressWarnings("unchecked")
    public static <T> T[][] toSixteenColsTable(T[] array) {
        int rowNum = 0;
        if (array.length % 16 == 0) {
            rowNum = array.length / 16;
        } else {
            rowNum = array.length / 16 + 1;
        }
        Class<T> ctype = (Class<T>) array.getClass().getComponentType();
        T[][] ret = (T[][]) Array.newInstance(ctype, rowNum, 16);
        int lastRowLength = 16;
        if (array.length % 16 != 0) {
            lastRowLength = array.length % 16;
            // ret[rowNum - 1] = (T[]) Array.newInstance(ctype, lastRowLength);
        }
        for (int i = 0; i < rowNum; i++) {
            int start = i * 16;
            int end = start + 16;
            if (i == rowNum - 1) {
                end = start + lastRowLength;
                for (int j = start, k = 0; j < end && k < lastRowLength; j++, k++) {
                    ret[i][k] = array[j];
                }
            } else {
                for (int j = start, k = 0; j < end && k < 16; j++, k++) {
                    ret[i][k] = array[j];
                }
            }
        }
        return ret;
    }

    // public static void main(String... args) {
    // Integer[][] result = toSixteenColsTable(new Integer[] { 1, 2, 3, 4, 5, 6,
    // 7, 8, 9, 10, 11, 12, 13,
    // 14, 15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 1, 2,
    // 3, 4, 5, 6, 7, 8, 9,
    // 10, 11, 12, 13, 14, 15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
    // 14, 15 });
    // for (Integer[] r : result) {
    // for (Integer i : r) {
    // System.out.print(i + " ");
    // }
    // System.out.println();
    // }
    // }
}
